package leetcode.code1353;

import java.util.Arrays;
import java.util.PriorityQueue;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {
	public int maxEvents(int[][] events) {
		Arrays.sort(events, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
		PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> a[1] - b[1]);
		int ans = 0, day = 1;
		int p = 0;
		while (p < events.length || !queue.isEmpty()) {
			while (p < events.length && (events[p][0] <= day || queue.isEmpty())) {
				queue.add(new int[] { events[p][0], events[p][1] });
				p++;
			}
			while (!queue.isEmpty() && queue.peek()[1] < day) {
				queue.poll();
			}
			if (!queue.isEmpty()) {
				ans++;
				int[] d = queue.poll();
				day = Math.max(day, d[0]);
				day++;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(16, this.maxEvents(H.str2array1(
				"[[1,1],[26,27],[17,17],[1,2],[4,7],[16,16],[20,23],[8,9],[17,19],[4,4],[15,15],[23,27],[28,31],[25,26],[25,29],[30,33]]")));
		H.compare(4, this.maxEvents(H.str2array1("[[1,3],[1,3],[1,3],[3,4]]")));
		H.compare(2, this.maxEvents(H.str2array1("[[52,79],[7,34]]")));
		H.compare(5, this.maxEvents(H.str2array1("[[1,5],[1,5],[1,5],[2,3],[2,3]]")));
		H.compare(5, this.maxEvents(H.str2array1("[[1,2],[1,2],[3,3],[1,5],[1,5]]")));
		H.compare(4, this.maxEvents(H.str2array1("[[1,4],[4,4],[2,2],[3,4],[1,1]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
